package com.example.goodog;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;

public class UpdateDogsLocationTask extends AsyncTask<String, Void, Boolean> {
	
	private final String NAMESPACE = "http://GooDog_Results.org/";
	private final String URL = "http://goodog.zapto.org/WebService1.asmx";
	private final String METHOD_NAME = "UpdateDogsLocation";
	
	@Override
	protected Boolean doInBackground(String... params) {

		String UpdateDogsLocationResult = UpdateDogsLocation(params[0],params[1],params[2]);
		
		if(!UpdateDogsLocationResult.equals("BAD"))
		{

			return true;
		}
		return false;
		
	}

	@Override
	protected void onPreExecute() {

	}

	@Override
	protected void onProgressUpdate(Void... values) {
	}
	
	
	private String UpdateDogsLocation(String DogIDs, String Lat, String Lon) {
	    //Create request
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
	    
	    request.addProperty("DogIDs",DogIDs);
	    request.addProperty("Lat",String.valueOf(Lat));
	    request.addProperty("Lon",String.valueOf(Lon));
	    
	    //Create envelope
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
	            SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    //Set output SOAP object
	    envelope.setOutputSoapObject(request);
	    //Create HTTP call object
	    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	 
	    try 
	    {	     
	        androidHttpTransport.call(NAMESPACE+METHOD_NAME, envelope);

            // Get the response
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            // Assign it to static variable
            return response.toString(); 
            
	    } 
	    catch (Exception e) 
	    {
	        e.printStackTrace();
	    }
	    return "BAD"; 
	}
}

